package com.genokon.util;

import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by baichaoda
 * @date 2020/11/3 13:36
 */
public class CheckParams {
    public static Map<String, String> checkNull(Object obj, List<String> params){
        Map<String, String> result = new HashMap<>(1);
        params.forEach(item->{
            String methodName = "get" + item.substring(0,1).toUpperCase() + item.substring(1);
            try {
                Method method = obj.getClass().getMethod(methodName);
                Object pramValue = method.invoke(obj);
                if (pramValue instanceof String){
                    if (StringUtils.isEmpty((String)pramValue)){
                        result.put(item, "空值");
                    }
                }else {
                    if (pramValue == null){
                        result.put(item, "空值");
                    }
                }
            } catch (Exception e) {
                result.put(item, "无此属性");
            }
        });
        return result;
    }
}
